﻿using System;
using System.Collections.Generic;
using System.Text;
using Meraz.Core.Parsers;
using Ninject;
using Meraz.Core.Config;
using Meraz.Core.Commands;
using Meraz.Core.Managers;
using Meraz.Core.Model;
using Meraz.Core.Repositories;
using Meraz.Core.Data;
namespace Meraz.Core.Factories
{
    public interface IObjectFactory
    {
        IKernel Kernel { get; set; }
        IMerazContext GetMerazContext();
        IBlogParser GetBlogParser(BlogKeyType blogKeyType);
        TwitterManager GetTwitterManager();
        IAddTwitterUserCommand GetAddTwitterUserCommand(int studyID, string username);
        ICreateTweetCommand GetCreateTweetCommand(TweetValues values);
        ICreateTwitterClientCommand GetCreateTwitterClientCommand(string clientName);
        ICreateTwitterUserCommand GetCreateTwitterUserCommand();
        IRemoveTwitterStudyCommand GetRemoveTwitterStudyCommand(int studyID);
        IRemoveTwitterUserCommand GetRemoveTwitterUserCommand(string username);
        IRemoveTwitterUserCommand GetRemoveTwitterUserCommand(string username, int studyID);
        ISaveDomainsCommand GetSaveDomainsCommand(IEnumerable<Domain> domains);
        ISaveTwitterStudyCommand GetSaveTwitterStudyCommand(string studyName);
        ISaveTwitterUserCommand GetSaveTwitterUserCommand(TwitterUser user);
        ISaveUrlRedirectCommand GetSaveUrlRedirectCommand(string redirect, string destination);
        IMemberRepository GetMemberRepository();
        IMemberRoleRepository GetMemberRoleRepository();
        IRoleRepository GetRoleRepository();
        bool Test();
    }
    public class ObjectFactory : IObjectFactory
    {
        [Inject]
        public IKernel Kernel { get; set; }

        public IMerazContext GetMerazContext()
        {
            return MerazContext.Instance;
        }
        public TwitterManager GetTwitterManager()
        {
            return Kernel.Get<TwitterManager>();
        }
        public IAddTwitterUserCommand GetAddTwitterUserCommand(int studyID, string username)
        {
            IAddTwitterUserCommand command = Kernel.Get<IAddTwitterUserCommand>();
            command.StudyID = studyID;
            command.UserName = username;
            return command;
        }
        public ICreateTweetCommand GetCreateTweetCommand(TweetValues values)
        {
            ICreateTweetCommand command = Kernel.Get<ICreateTweetCommand>();
            command.Values = values;
            return command;
        }
        public ISaveDomainsCommand GetSaveDomainsCommand(IEnumerable<Domain> domains)
        {
            ISaveDomainsCommand command = Kernel.Get<ISaveDomainsCommand>();
            command.Result = new List<Domain>(domains);
            return command;
        }

        public ICreateTwitterClientCommand GetCreateTwitterClientCommand(string clientName)
        {
            ICreateTwitterClientCommand command = Kernel.Get<ICreateTwitterClientCommand>();
            command.Result = new TwitterClient() { Name = clientName };
            return command;
        }
        public ICreateTwitterUserCommand GetCreateTwitterUserCommand()
        {
            ICreateTwitterUserCommand command = Kernel.Get<ICreateTwitterUserCommand>();
            return command;
        }
        public IRemoveTwitterStudyCommand GetRemoveTwitterStudyCommand(int studyID)
        {

            IRemoveTwitterStudyCommand command = Kernel.Get<IRemoveTwitterStudyCommand>();
            command.StudyID = studyID;
            return command;
        }
        public IRemoveTwitterUserCommand GetRemoveTwitterUserCommand(string username)
        {
            return GetRemoveTwitterUserCommand(username, 0);
        }
        public IRemoveTwitterUserCommand GetRemoveTwitterUserCommand(string username, int studyID)
        {
            IRemoveTwitterUserCommand command = Kernel.Get<IRemoveTwitterUserCommand>();
            command.UserName = username;
            command.StudyID = studyID;
            return command;
        }
        public ISaveTwitterStudyCommand GetSaveTwitterStudyCommand(string studyName)
        {
            ISaveTwitterStudyCommand command = Kernel.Get<ISaveTwitterStudyCommand>();
            TwitterStudy study = new TwitterStudy() { Name = studyName};
            command.Study = study;
            return command;
        }
        public ISaveTwitterUserCommand GetSaveTwitterUserCommand(TwitterUser user)
        {

            ISaveTwitterUserCommand command = Kernel.Get<ISaveTwitterUserCommand>();
            command.Result = user;
            return command;
        }
        public ISaveUrlRedirectCommand GetSaveUrlRedirectCommand(string redirect, string destination)
        {
            ISaveUrlRedirectCommand command = Kernel.Get<ISaveUrlRedirectCommand>();
            command.Redirect = redirect;
            command.Destination = destination;
            return command;
        }

        public IMemberRepository GetMemberRepository()
        {
            return Kernel.Get<IMemberRepository>();
        }
        public IMemberRoleRepository GetMemberRoleRepository()
        {
            return Kernel.Get<IMemberRoleRepository>();
        }
        public IRoleRepository GetRoleRepository()
        {
            return Kernel.Get<IRoleRepository>();
        }


        public bool Test()
        {
            return (this.Kernel != null);
        }

        public IBlogParser GetBlogParser(BlogKeyType blogKeyType)
        {
            IBlogParser parser = null;
            switch (blogKeyType)
            {
                case BlogKeyType.DailyKos: parser = new DailyKosParser(); break;
                case BlogKeyType.FireDogLake: parser = new FireDogLakeParser(); break;
                case BlogKeyType.HotAir: parser = new HotAirParser(); break;
                case BlogKeyType.HuffingtonPost: parser = new HuffingtonPostParser(); break;
                case BlogKeyType.MichelleMalkin: parser = new MichelleMalkinParser(); break;
                case BlogKeyType.TalkingPointsMemo: parser = new TalkingPointsMemoParser(); break;
                case BlogKeyType.ThinkProgress: parser = new ThinkProgressParser(); break;
                case BlogKeyType.Unknown: parser = new GenericParser(); break;
                default: break;
            }
            if (parser == null)
                throw new NotImplementedException(string.Format("Parser Type '{0}' not implemented.", blogKeyType.ToString()));
            return parser;
        }
    }
}